from typing import List
from database.task import TaskTable
from database.taskrecord import TaskRecordTable
from database.parkrecord import ParkRecordTable
from database.user import UserTable
from models.query import TaskQuery, ParkQuery
from models.task import DbTask, UiTask
from models.taskrecord import DbTaskRecord, UiTaskRecord
from models.parkrecord import DbParkRecord, UiParkRecord
from models.user import User
from utils.time import Date2Str

def taskid2name(id : int):
    with TaskTable() as db:
        return db.getTask(id).name

def userid2name(id : int):
    with UserTable() as db:
        return db.getUser(id).username
    
def dbtask2uitask(dbtasks : List[DbTask]):
    uitasks = []
    for dbtask in dbtasks:
        uitask = UiTask(dbtask)
        uitask.user_name = userid2name(dbtask.user_id)
        uitask.create_time = Date2Str(dbtask.gmt_create)
        uitask.update_time = Date2Str(dbtask.gmt_modified)
        uitasks.append(uitask)
    return uitasks

def dbtaskrecord2uitaskrecord(dbtaskrecords : List[DbTaskRecord]):
    uitaskrecords = []
    for dbtaskrecord in dbtaskrecords:
        uitaskrecord = UiTaskRecord(dbtaskrecord)
        uitaskrecord.create_time = Date2Str(dbtaskrecord.gmt_create)
        uitaskrecord.update_time = Date2Str(dbtaskrecord.gmt_modified)
        uitaskrecord.user_name = userid2name(dbtaskrecord.user_id)
        uitaskrecord.task_name = taskid2name(dbtaskrecord.task_id)
        uitaskrecords.append(uitaskrecord)
    return uitaskrecords

def dbparkrecord2uiparkrecord(dbparkrecords : List[DbParkRecord]):
    uiparkrecords = []
    for dbparkrecord in dbparkrecords:
        uiparkrecord = UiParkRecord(dbparkrecord)
        uiparkrecord.create_time = Date2Str(dbparkrecord.gmt_create)
        uiparkrecord.update_time = Date2Str(dbparkrecord.gmt_modified)
        uiparkrecord.entry_time_str = Date2Str(dbparkrecord.entry_time)
        uiparkrecord.user_name = userid2name(dbparkrecord.user_id)
        uiparkrecord.task_name = taskid2name(dbparkrecord.task_id)
        uiparkrecords.append(uiparkrecord)
    return uiparkrecords

def task(query : TaskQuery, user: User):
    with TaskTable() as db:
        if not user.isadmin(): query.user_id = user.id
        total = db.query(query, True)
        items = dbtask2uitask(db.query(query, False))
        return {"code": 20000,"data": {"total": total,"items": items}}

def taskrecord(query : TaskQuery, user: User):
    with TaskRecordTable() as db:
        if not user.isadmin(): query.user_id = user.id
        total = db.query(query, True)
        items = dbtaskrecord2uitaskrecord(db.query(query, False))
        return {"code": 20000,"data": {"total": total,"items": items}}
    
def parkrecord(query : ParkQuery, user: User):
    with ParkRecordTable() as db:
        if not user.isadmin(): query.user_id = user.id
        total = db.query(query, True)
        items = dbparkrecord2uiparkrecord(db.query(query, False))
        return {"code": 20000,"data": {"total": total,"items": items}}